using GEditor.import;
using GEditor.View;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace GEditor
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private GWinformLib.Viewport viewport = new GWinformLib.Viewport();

        private Outliner outliner = new Outliner();
        const  string TemplateProjectName = @"Resource\Template.d";
        const  string TempProjectName = @"Resource\temp.d";
        bool mIsNewProj = false;

        Properties_SceneObject properties_SceneObject;

        public MainWindow()
        {
            InitializeComponent();

           
            viewport.BorderStyle = BorderStyle.None;

            wfHost.Child = viewport;
            OutlinerPanel.Children.Add(outliner);
            outliner.SetMainWindow(this);

            //             string a = "qwe\\asd\\zc.f";
            //             string b = "qwe\\";
            //             string c = a.Replace(b,null);
                  }

        private void wfHost_ChildChanged(object sender, System.Windows.Forms.Integration.ChildChangedEventArgs e)
        {

            
        }

        public void GetSceneObjectProperties(string objName)
        {
            PropertiesPanel.Children.Clear();
            properties_SceneObject = new Properties_SceneObject();
            properties_SceneObject.SetMainWindow(this);
            properties_SceneObject.SetObjectName(objName);
            PropertiesPanel.Children.Add(properties_SceneObject);
            properties_SceneObject.GetSceneObjectProperties();
        }

        private void NewProject(object sender, RoutedEventArgs e)
        {
            mIsNewProj = true;

            string path = TemplateProjectName;
            string path2 = TempProjectName;
            FileInfo fi1 = new FileInfo(path);
            FileInfo fi2 = new FileInfo(path2);

            try
            {

                if (File.Exists(path2))
                {
                    fi2.Delete();
                }
                fi1.CopyTo(path2);
            }
            catch (IOException ioex)
            {
                System.Windows.MessageBox.Show(ioex.Message);

            }

            RunProject(path2);
        }

       
        private void OpenProject(object sender, RoutedEventArgs e)
        {
            // Create OpenFileDialog 
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            dlg.InitialDirectory = @"Project\";
            // Set filter for file extension and default file extension 
            dlg.DefaultExt = ".data";
            dlg.Filter = "Project(.data)|*.data";

            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> result = dlg.ShowDialog();

            if (result == true)
            {
                RunProject(dlg.FileName);
            }
        }
        private void RunProject(string projName)
        {
            IGCore.SetProjectName(projName);

            IntPtr hwnd = viewport.Handle;
            double h = viewport.Height;
            double w = viewport.Width;
            IGCore.InitD3D(hwnd, w, h);


            outliner.GetSceneObjects();

            IGCore.RenderFrame();
        }
        private void SaveProject(object sender, RoutedEventArgs e)
        {
            IGCore.SaveScene();
            if(mIsNewProj)
            {
                mIsNewProj = false;

                Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
                dlg.InitialDirectory = @"Project\";

                dlg.FileName = "Project"; // Default file name
                dlg.DefaultExt = ".data";
                dlg.Filter = "Project(.data)|*.data";

                // Show save file dialog box
                Nullable<bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    string filename = dlg.FileName;

                   
                    FileInfo fi2 = new FileInfo(TempProjectName);

                    try
                    {
                        fi2.MoveTo(filename);
                    }
                    catch (IOException ioex)
                    {
                        System.Windows.MessageBox.Show(ioex.Message);
                    }
                    
                }
            }
        }

        private void ProjectSettings(object sender, RoutedEventArgs e)
        {

        }

        private void wfHost_MouseDown(object sender, MouseButtonEventArgs e)
        {
           
        }
        static bool openProj = true;
        private void OutlinerPanel_MouseDown(object sender, MouseButtonEventArgs e)
        {
            
            if (openProj)
            {
                openProj = false;
                RunProject(@"Project.data");
            }
//             MessageBoxResult res = System.Windows.MessageBox.Show("打开上次的项目");
//             if(res == System.Windows.MessageBoxResult.OK)
//             {
//                
//             }
//             int a = 1;
        }

        uint mInI;
        enum INI
        {
            INI_UseSky = 1 << 0,
            INI_UseDeferredOut = 1 << 1,
            INI_UseIMGUI = 1 << 2
        };
        private void Sky(object sender, RoutedEventArgs e)
        {
            mInI ^= (uint)INI.INI_UseSky;
            IGCore.SetExternData(mInI);
        }

        private void DeferredBebug(object sender, RoutedEventArgs e)
        {
            mInI ^= (uint)INI.INI_UseDeferredOut;
            IGCore.SetExternData(mInI);
        }

        private void ImGui(object sender, RoutedEventArgs e)
        {
            
            mInI ^= (uint)INI.INI_UseIMGUI;
            IGCore.SetExternData(mInI);
        }
    }
}
